﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Entitys;
using Shop.Interface.Repository;
using Shop.Models.Dto.User;
using Shop.Services.Repository;
using SqlSugar;

namespace Shop.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ToolController : ControllerBase
    {
        private readonly IBaseRepository _repository;
        public ToolController(IBaseRepository repository) 
        { 
            _repository = repository;
        }

        [HttpGet]
        public async Task<bool> CodeFirst()
        {
            //创建超级管理员菜单
            var user = new Users
            {
                Id = Guid.NewGuid().ToString(),
                Name = "admin",
                NickName = "超级管理员",
                Password = "123456",
                UserType = 0,
                IsEnable = true,
                Description = "数据库初始化时添加的超级管理员",
                CreateDate = DateTime.Now,
                CreateUserId = "",
            };
            var result= await _repository.CreateAsync(user);
            var menu = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "菜单管理",
                Index = "/menu",
                FilePath = "menu.vue",
                ParentId = "",
                Order =1,
                IsEnable = true,
                Icon ="folder",
                Description = "数据库初始化时添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id,
            };
            result = await _repository.CreateAsync(menu);
            var m1 = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "菜单列表",
                Index = "/menu",
                FilePath = "menu.vue",
                ParentId = menu.Id,
                Order = 1,
                IsEnable = true,
                Icon = "notebook",
                Description = "数据库初始化时默认添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id
            };
            result= await _repository.CreateAsync(m1);
            var m2 = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "角色管理",
                Index = "/role",
                FilePath = "role.vue",
                ParentId = "",
                Order = 1,
                IsEnable = true,
                Icon = "folder",
                Description = "数据库初始化时默认添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id
            };
            result = await _repository.CreateAsync(m2);
            var m22 = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "角色列表",
                Index = "/role",
                FilePath = "role.vue",
                ParentId = m2.Id,
                Order = 1,
                IsEnable = true,
                Icon = "notebook",
                Description = "数据库初始化时默认添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id
            };
            result= await _repository.CreateAsync(m22);
            var m3 = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "用户管理",
                Index = "/user",
                FilePath = "user.vue",
                ParentId = "",
                Order = 1,
                IsEnable = true,
                Icon = "folder",
                Description = "数据库初始化时默认添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id
            };
            result= await _repository.CreateAsync(m3);
            var m33 = new Menu()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "用户列表",
                Index = "/user",
                FilePath = "user.vue",
                ParentId = m3.Id,
                Order = 1,
                IsEnable = true,
                Icon = "notebook",
                Description = "数据库初始化时默认添加的默认菜单",
                CreateDate = DateTime.Now,
                CreateUserId = user.Id
            };
            result= await _repository.CreateAsync(m33);
            return result;
        }
    }
}
